﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Eb.StockManagement;
using Purchase;
using Eb.MessageBox;
using System.Drawing;
public partial class Modules_StockManagement_IssueForProductionBasedonBOM : System.Web.UI.UserControl
{
    Purchase.Purchase purchaseMgmt = new Purchase.Purchase();
    StockManagement stockMgmt = new StockManagement();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GetProducts();
            txtProductNo.Text = "1";
        }
    

    }

    protected void GetProducts()
    {
        DataSet productDS = purchaseMgmt.GetProducts();
        ddlProductName.DataSource = productDS;
        ddlProductName.DataValueField = "ProductID";
        ddlProductName.DataTextField = "ProductName";
        ddlProductName.DataBind();
        ddlProductName.Items.Insert(0, "-Select-");
    }
    protected void ddlProductName_SelectedIndexChanged(object sender, EventArgs e)
    {
        txtProductNo.Text = "1";

        txtProductNo.Visible = true;
        lblProductName.Visible = true;
        int productID = 0;
        if (ddlProductName.SelectedIndex != 0)
            productID = Convert.ToInt16(ddlProductName.SelectedValue);

        if (ddlProductName.SelectedIndex == 0)
        {
            grvMaterialDetails.DataSource = null;
            grvMaterialDetails.DataBind();
        }
        else
        {
            if (productID != 0)
            {
                DataSet materialDS = purchaseMgmt.GetMaterialDetailsByProductID(productID);
                grvMaterialDetails.DataSource = materialDS;
                grvMaterialDetails.DataBind();
            }
        }
    }
    protected void grvMaterialDetails_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        int productQty = 0;

        if (txtProductNo.Text.Trim() != "" && txtProductNo.Text != null)
            productQty = Convert.ToInt32(txtProductNo.Text.Trim());

        int qty = 0;
        foreach (GridViewRow gvr in grvMaterialDetails.Rows)
        {
            Label unitQty = (Label)gvr.Cells[3].FindControl("lblUnitQty");
            int uQty = Convert.ToInt32(unitQty.Text);
            Label tbQty = (Label)gvr.Cells[2].FindControl("lblTotQty"); //get the quantity TextBox
            tbQty.Text = Convert.ToString(uQty * productQty);

        }
    }
   
    
    protected void txtProductNo_TextChanged(object sender, EventArgs e)
    {
      

        int productQty = 0;

        if (txtProductNo.Text.Trim() != "" && txtProductNo.Text != null)
            productQty = Convert.ToInt32(txtProductNo.Text.Trim());

        int qty = 0;
        foreach (GridViewRow gvr in grvMaterialDetails.Rows)
        {
            Label unitQty = (Label)gvr.Cells[3].FindControl("lblUnitQty");
            int uQty = Convert.ToInt32(unitQty.Text);
            Label tbQty = (Label)gvr.Cells[2].FindControl("lblTotQty"); //get the quantity TextBox
            tbQty.Text = Convert.ToString(uQty * productQty);

        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        int matUniqID = 0;
        if (grvMaterialDetails.Rows.Count<=0)
                MessageBox.Show("No Items in this List..Choose another Product.");
        else
        {
            // bool status1 = true;
            bool materialStatus = false;
            int userId = 0;
            if (Session["UserId"] != null)
                userId = Convert.ToInt16(Session["UserId"].ToString());
            bool status = CheckQuantity();
            if (!status)
            {
                MessageBox.Show("Invalid Quantity!!Not enough items in stock");
            }
            else
            {
                int productID = 0;
                int productQty = 0;

                if (ddlProductName.SelectedIndex > 0)
                    productID =Convert.ToInt16(ddlProductName.SelectedValue);
                if (txtProductNo.Text.Trim() != "" && txtProductNo.Text != null)
                    productQty = Convert.ToInt32(txtProductNo.Text.Trim());
                matUniqID = stockMgmt.InsertMaterialBatchHistory(userId, productID, 2);

                foreach (GridViewRow gvr in grvMaterialDetails.Rows)
                {
                    Label txtMaterialID = (Label)gvr.Cells[3].FindControl("lblMaterialID");
                    int materialID = Convert.ToInt32(txtMaterialID.Text);
                    TextBox txtReqQty = (TextBox)gvr.Cells[2].FindControl("txtRequiredQty");
                    int requiredQty = Convert.ToInt32(txtReqQty.Text);
                    bool status1 = stockMgmt.InsertMaterialHistoryBatchWise(userId, materialID, requiredQty, matUniqID);
                    if (status1)
                    {
                        materialStatus = stockMgmt.UpdateMaterialStock(materialID, requiredQty);
                    }
                }

                if (materialStatus)
                {
                    MessageBox.Show("Successfully Issued");
                    ClearControls();
                }

            }
        }

    }
    protected bool CheckQuantity()
    {
        int productQty = 0;
        bool status = true;
        if (txtProductNo.Text.Trim() != "" && txtProductNo.Text != null)
            productQty = Convert.ToInt32(txtProductNo.Text.Trim());
        foreach (GridViewRow gvr in grvMaterialDetails.Rows)
        {
            Label tbQty = (Label)gvr.Cells[2].FindControl("lblCurrentQty");
            int totQty = Convert.ToInt32(tbQty.Text);
            TextBox txtReqQty = (TextBox)gvr.Cells[2].FindControl("txtRequiredQty");
            int requiredQty = Convert.ToInt32(txtReqQty.Text);
            if (requiredQty > totQty)
            {
                status = false;
                txtReqQty.BorderColor = Color.Red;

                //break;
            }

        }
        return status;
    }
    protected void txtRequiredQty_onTextIndexChanged(object sender, EventArgs e)
    {
       string ff= ((TextBox)sender).Text;
       
    }
    protected void chkSelectAll_CheckedChanged(object sender, EventArgs e)
    {
        CheckBox chkAll = (CheckBox)grvMaterialDetails.HeaderRow.FindControl("chkSelect");
        if (chkAll.Checked == true)
        {
            foreach (GridViewRow gvr in grvMaterialDetails.Rows)
            {
                int productQty = 0;
                int materialQty = 0;
                if (txtProductNo.Text.Trim() != "" && txtProductNo.Text != null)
                    productQty = Convert.ToInt32(txtProductNo.Text.Trim());

                TextBox txtReqQty = (TextBox)gvr.Cells[2].FindControl("txtRequiredQty");
                Label tbQty = (Label)gvr.Cells[2].FindControl("lblUnitQty");
                materialQty = Convert.ToInt32(tbQty.Text);
                txtReqQty.Text = Convert.ToString(materialQty * productQty);
            }
        }
        else
        {
            foreach (GridViewRow gvr in grvMaterialDetails.Rows)
            {
                TextBox txtReqQty = (TextBox)gvr.Cells[2].FindControl("txtRequiredQty");
                txtReqQty.Text = "0";
            }
        }
    }

    protected void ClearControls()
    {
        ddlProductName.SelectedIndex = 0;
        txtProductNo.Text = "1";
        grvMaterialDetails.DataSource = null;
        grvMaterialDetails.DataBind();
    }
}
